# -*- coding:utf-8 -*-
"""'' 
代码块的功能是利用sqlit3链接数据库，并且创建用户表。 
插入数据，进行查询，然后再利用输入的数字大小的区间， 
返回出符合区间之间的姓名、 
"""
import os
import sqlite3

db_file = os.path.join(os.path.dirname(__file__), 'scored.db')
if os.path.isfile(db_file):
    os.remove(db_file)

# 初始化数据
conn = sqlite3.connect(db_file)
cursor = conn.cursor()
cursor.execute('CREATE TABLE user (id VARCHAR(20) PRIMARY KEY, name VARCHAR(20), score INT)')
cursor.execute(r"INSERT INTO user VALUES ('A-001', 'Adam', 95)")
cursor.execute(r"INSERT INTO user VALUES ('A-002', 'Bart', 62)")
cursor.execute(r"INSERT INTO user VALUES ('A-003', 'Lisa', 78)")
print('插入%s 行数据' % cursor.rowcount)
cursor.close()
conn.commit()
conn.close()


def get_score_in(low, high):
    conn_ = sqlite3.connect(db_file)
    cursor_ = conn_.cursor()
    cursor_.execute('SELECT name FROM user WHERE score BETWEEN ? AND ? ORDER BY score ASC', (low, high))
    records = cursor_.fetchall()
    cursor_.close()
    conn_.close()
    names = [x[0] for x in records]
    return names


#  debug代码：
print('接下来为调试部分\n')
print('调试开始----------》\n')
assert get_score_in(80, 95) == ['Adam'], get_score_in(80, 95)
assert get_score_in(60, 80) == ['Bart', 'Lisa'], get_score_in(60, 80)
assert get_score_in(60, 100) == ['Bart', 'Lisa', 'Adam'], get_score_in(60, 100)
print('调试结束----------》\n')

res1 = get_score_in(80, 95)
res2 = get_score_in(60, 80)
res3 = get_score_in(60, 100)

#  打印
print(res1)
print(res2)
print(res3)
